<form id="edit-form" class="form-horizontal" style="position:relative;background: #fff" role="form"
    data-toggle="validator" method="POST" action="">

    <div class="panel-body">
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade active in" id="tabs_0">
                <input TYPE="hidden" id="row" NAME="data" VALUE='{$row|json_encode|htmlentities}' />
                <div class="form-group">
                    <label for="c-subjectlv_tree" class="control-label col-xs-12 col-sm-2">父级:</label>
                    <div class="col-xs-12 col-sm-8" id="div-subjectlv_tree">
                        <select name="row[pid]" id="c-subjectlv_tree" class="form-control">
                        </select>
                    </div>
                </div>
                <input type="hidden" id="c-level" name="row[level]" value="{$row.level}" class="form-control" />

                <div class="form-group">
                    <label for="c-name" class="control-label col-xs-12 col-sm-2">学科名称:</label>
                    <div class="col-xs-12 col-sm-8" id="div-name">
                        <input type="text" id="c-name" name="row[name]" value="{$row.name}" class="form-control"
                            title="name" placeholder="" data-rule="" data-tip="学科名称" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="c-addresslv_id_tree" class="control-label col-xs-12 col-sm-2">对应地址等级:</label>
                    <div class="col-xs-12 col-sm-8" id="div-address_lv">
                        <select id="c-addresslv_id_tree" name="row[addresslv_id]" class="form-control" data-rule=""
                            data-tip="地址等级">
                            <option value="">请选择地址等级</option>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label for="c-status" class="control-label col-xs-12 col-sm-2">状态:</label>
                    <div class="col-xs-12 col-sm-8" id="div-status">
                        <input id="c-status" name="row[status]" type="hidden" value="{$row.status}">
                        <a href="javascript:;" data-toggle="switcher" class="btn-switcher" data-input-id="c-status"
                            data-yes="1" data-no="0">
                            <i class="fa fa-toggle-on text-success fa-flip-horizontal fa-2x"></i>
                        </a>
                        <div data-favisible="switch=1" class="p-3">已开户</div>
                    </div>
                </div>
                <div class="form-group layer-footer">
                    <label class="control-label col-xs-12 col-sm-2"></label>
                    <div class="col-xs-12 col-sm-8">
                        <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
                        <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
                    </div>
                </div>
            </div>

        </div>
    </div>
</form>

<script>
    // 确保jQuery已经加载
    if (typeof jQuery === 'undefined') {
        document.write('<script src="/assets/js/jquery.min.js"><\/script>');
    }

    // 页面加载完成后执行
    $(document).ready(function () {
        // 获取当前的addresslv_id值
        var currentAddresslvId = "{$row.addresslv_id}";

        // 加载地址等级数据
        if ($("#c-addresslv_id_tree").length > 0) {
            $.ajax({
                url: '/addresslv/addresslv/index',
                type: 'GET',
                dataType: 'json',
                data: {
                    limit: 1000  // 获取所有数据
                },
                success: function (response) {
                    if (response && response.rows) {
                        var $select = $("#c-addresslv_id_tree");
                        $select.empty();
                        $select.append('<option value="">请选择地址等级</option>');

                        // 添加选项
                        $.each(response.rows, function (index, item) {
                            var selected = (item.id == currentAddresslvId) ? 'selected' : '';
                            $select.append('<option value="' + item.id + '" ' + selected + '>' + item.name + '</option>');
                        });

                        // 触发change事件
                        $select.trigger("change2");
                    }
                },
                error: function () {
                    console.error('加载地址等级数据失败');
                }
            });
        }

        // 如果存在父级下拉框，触发change事件加载数据
        if ($("#c-subjectlv_tree").length > 0) {
            $("#c-subjectlv_tree").trigger("change");
        }
    });
</script>